package cn.yinyu.queue.module.ecg.dal.mysql.jobrecord;

import java.time.LocalDateTime;
import java.util.*;

import cn.yinyu.queue.framework.common.pojo.PageResult;
import cn.yinyu.queue.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.yinyu.queue.framework.mybatis.core.mapper.BaseMapperX;
import cn.yinyu.queue.module.ecg.dal.dataobject.jobrecord.JobRecordDO;
import cn.yinyu.queue.module.ecg.dal.dataobject.jobrecord.JobRecordStatisticDO;
import org.apache.ibatis.annotations.Mapper;
import cn.yinyu.queue.module.ecg.controller.admin.jobrecord.vo.*;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

/**
 * 工作记录 Mapper
 *
 * @author 垠域源码
 */
@Mapper
public interface JobRecordMapper extends BaseMapperX<JobRecordDO> {

    default PageResult<JobRecordDO> selectPage(JobRecordPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<JobRecordDO>()
                //.eqIfPresent(JobRecordDO::getJobType, reqVO.getJobType())
                .inIfPresent(JobRecordDO::getJobType, reqVO.getJobType())
                .eqIfPresent(JobRecordDO::getDocId, reqVO.getDocId())
                .likeIfPresent(JobRecordDO::getDocName, reqVO.getDocName())
                .eqIfPresent(JobRecordDO::getDevId, reqVO.getDevId())
                .eqIfPresent(JobRecordDO::getPatId, reqVO.getPatId())
                .likeIfPresent(JobRecordDO::getPatName, reqVO.getPatName())
                .betweenIfPresent(JobRecordDO::getJobTime, reqVO.getJobTime())
                .eqIfPresent(JobRecordDO::getSummary, reqVO.getSummary())
                .eqIfPresent(JobRecordDO::getRemark, reqVO.getRemark())
                .betweenIfPresent(JobRecordDO::getCreateTime, reqVO.getCreateTime())
                .eqIfPresent(JobRecordDO::getRentId, reqVO.getRentId())
                .orderByDesc(JobRecordDO::getId));
    }

    @Select("<script> " +
            "SELECT * FROM yy_queue.job_record " +
            " where rent_id=#{rentId} and doc_id=#{docId} and job_type in (" +
                    " <foreach collection='jobTypeList' separator=',' item='jobType'>" +
                    "  #{jobType} " +
                    " </foreach> )" +
            " limit 1;" +
            " </script>")
    JobRecordDO selectByRentIdDocId(@Param("rentId") Long rentId, @Param("docId") Long docId, @Param("jobTypeList") List<Integer> jobTypeList );

    @Select("SELECT count(1) FROM yy_queue.job_record where pat_id=#{patId} and TO_DAYS(job_time)=TO_DAYS(#{jobTime}) and job_type=#{jobType} ")
    Integer getPatientJobNum(@Param("patId") String patId, @Param("jobTime") LocalDateTime jobTime, @Param("jobType") Integer jobType );

    @Select("select year(job_time) as year, month(job_time) as month, doc_id, doc_name, job_type, count(1) as job_count from yy_queue.job_record " +
            "where job_time >= #{jobTimeFrom} and job_time < #{jobTimeTo} \n" +
            "group by year(job_time), month(job_time), doc_id, job_type;")
    List<JobRecordStatisticDO> getJobStatistic(@Param("jobTimeFrom") LocalDateTime jobTimeFrom, @Param("jobTimeTo") LocalDateTime jobTimeTo);
}
